/*
 * jxDao Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxDao.dao.db;

import java.io.Serializable;

import org.jxUtils.check.ParameterCheck;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxDao
 * @package org.jxDao.dao.db
 * @date 11/04/2014 - 13:26:25
 */
public enum URL implements Serializable {
	
	/**
	 * params:
	 * 
	 * @DATABASE
	 */
	DERBY("jdbc:derby:@DATABASE;create=true"),
	
	/**
	 * params:
	 * 
	 * @HOST
	 * @PORT
	 * @DATABASE
	 */
	FIREBIRD("jdbc:firebirdsql:@HOST/@PORT:@DATABASE"),
	
	/**
	 * params:
	 * 
	 * @HOST
	 * @PORT
	 * @DATABASE
	 * @USER
	 * @PASSWORD
	 */
	MYSQL("jdbc:mysql://@HOST:@PORT/@DATABASE?user=@USER&password=@PASSWORD"),
	
	/**
	 * params:
	 * 
	 * @HOST
	 * @PORT
	 * @SID
	 */
	ORACLE("jdbc:oracle:thin:@@HOST:@PORT:@SID"),
	
	/**
	 * params:
	 * 
	 * @HOST
	 * @PORT
	 * @DATABASE
	 */
	POSTGRESQL("jdbc:postgresql://@HOST:@PORT/@DATABASE"),
	
	/**
	 * params:
	 * 
	 * @DATABASE
	 */
	SQLITE("jdbc:sqlite:@DATABASE"),
	
	/**
	 * params:
	 * 
	 * @HOST
	 * @PORT
	 * @DATABASE
	 * @INSTANCE
	 * @USER
	 * @PASSWORD
	 */
	SQLSERVER("jdbc:sqlserver://@HOST:@PORT;databaseName=@DATABASE;instanceName=@INSTANCE;user=@USER;password=@PASSWORD");
	
	private final String url;
	
	/**
	 * @param url
	 */
	URL(final String url) {
	
		this.url = url;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Enum#toString()
	 */
	@Override
	public String toString() {
	
		return url;
	}
	
	/**
	 * @param dbInfo
	 * @return url or empty String
	 */
	public static String getUrl(final DBInfo dbInfo) {
	
		ParameterCheck.isNull(dbInfo, "dbInfo");
		
		//
		final String host = dbInfo.getHost() != null ? dbInfo.getHost().trim() : "";
		final String port = dbInfo.getPort() != null && !dbInfo.getPort().trim().isEmpty() ? ":" + dbInfo.getPort().trim() : "";
		final String database = dbInfo.getDatabase() != null ? dbInfo.getDatabase().trim() : "";
		final String user = dbInfo.getUser() != null ? dbInfo.getUser().trim() : "";
		final String password = dbInfo.getPassword() != null ? dbInfo.getPassword().trim() : "";
		
		//
		if(dbInfo.getDBMS().getDriver().equals(Driver.DERBY)) {
			
			return URL.DERBY.toString().replace("@DATABASE", database);
			
		}else if(dbInfo.getDBMS().getDriver().equals(Driver.FIREBIRD)) {
			
			return URL.FIREBIRD.toString().replace("@HOST", host).replace("/@PORT", port.replace(":", "/")).replace("@DATABASE", database);
			
		}else if(dbInfo.getDBMS().getDriver().equals(Driver.MYSQL)) {
			
			return URL.MYSQL.toString().replace("@HOST", host).replace(":@PORT", port).replace("@DATABASE", database).replace("@USER", user).replace("@PASSWORD", password);
			
		}else if(dbInfo.getDBMS().getDriver().equals(Driver.ORACLE)) {
			
			return URL.ORACLE.toString().replace("@HOST", host).replace(":@PORT", port).replace("@SID", database);
			
		}else if(dbInfo.getDBMS().getDriver().equals(Driver.POSTGRESQL)) {
			
			return URL.POSTGRESQL.toString().replace("@HOST", host).replace(":@PORT", port).replace("@DATABASE", database);
			
		}else if(dbInfo.getDBMS().getDriver().equals(Driver.SQLITE)) {
			
			return URL.SQLITE.toString().replace("@DATABASE", database);
			
		}else {
			
			final String[] hostInstance = host.replace("\\", "#").replace("/", "#").split("#");
			
			return URL.SQLSERVER.toString().replace("@HOST", hostInstance[0]).replace(":@PORT", port).replace("@DATABASE", database).replace(";instanceName=@INSTANCE", hostInstance.length > 1 ? ";instanceName=" + hostInstance[1] : "").replace("@USER", user).replace("@PASSWORD", password);
		}
	}
}
